﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using HYElectricCommon;

namespace HYRElectric.PowerSystem
{
	public class DBhelper
	{
		public static SQLiteConnection Connection
		{
			get
			{
				string fileName = AssistFunc.GetAppRootPath() + "\\Data\\StandardManual.db";
				string text = Path.Combine(Product.WorkingLocation, "StandardManual.db");
				if (DataBaseTempFileOperate.IsExistFile(text))
				{
					if (!DataBaseTempFileOperate.IsSameVersion(fileName, text))
					{
						DataBaseTempFileOperate.CopyDBFile(fileName, Product.WorkingLocation);
					}
				}
				else
				{
					DataBaseTempFileOperate.CopyDBFile(fileName, Product.WorkingLocation);
				}
				string dataSource = text;
				SQLiteConnectionStringBuilder sqliteConnectionStringBuilder = new SQLiteConnectionStringBuilder();
				sqliteConnectionStringBuilder.DataSource = dataSource;
				DBhelper.connection = new SQLiteConnection();
				DBhelper.connection.ConnectionString = sqliteConnectionStringBuilder.ToString();
				try
				{
					if (DBhelper.connection == null)
					{
						DBhelper.connection.Open();
					}
					else if (DBhelper.connection.State == ConnectionState.Closed)
					{
						DBhelper.connection.Open();
					}
					else if (DBhelper.connection.State == ConnectionState.Broken)
					{
						DBhelper.connection.Close();
						DBhelper.connection.Open();
					}
				}
				catch (Exception ex)
				{
					string message = ex.Message;
					return null;
				}
				return DBhelper.connection;
			}
		}

		public static double GetReader(string safeSql)
		{
			SQLiteCommand sqliteCommand = new SQLiteCommand(safeSql, DBhelper.Connection);
			double result;
			try
			{
				string s = "";
				using (SQLiteDataReader sqliteDataReader = sqliteCommand.ExecuteReader())
				{
					while (sqliteDataReader.Read())
					{
						if (sqliteDataReader[0] != null)
						{
							s = sqliteDataReader[0].ToString();
						}
					}
					sqliteDataReader.Dispose();
					DBhelper.connection.Close();
					result = double.Parse(s);
				}
			}
			catch (Exception)
			{
				result = -1.0;
			}
			return result;
		}

		public static string GetReaderStr(string safeSql)
		{
			SQLiteCommand sqliteCommand = new SQLiteCommand(safeSql, DBhelper.Connection);
			string result;
			try
			{
				string text = "";
				using (SQLiteDataReader sqliteDataReader = sqliteCommand.ExecuteReader())
				{
					while (sqliteDataReader.Read())
					{
						if (sqliteDataReader[0] != null)
						{
							text = sqliteDataReader[0].ToString();
						}
					}
					sqliteDataReader.Dispose();
					DBhelper.connection.Close();
					result = text;
				}
			}
			catch (Exception)
			{
				result = "";
			}
			return result;
		}

		public static List<string> GetReaderList(string safeSql)
		{
			List<string> list = new List<string>();
			SQLiteCommand sqliteCommand = new SQLiteCommand(safeSql, DBhelper.Connection);
			List<string> result;
			try
			{
				using (SQLiteDataReader sqliteDataReader = sqliteCommand.ExecuteReader())
				{
					while (sqliteDataReader.Read())
					{
						if (sqliteDataReader[0] != null)
						{
							list.Add(sqliteDataReader[0].ToString());
						}
					}
					sqliteDataReader.Dispose();
					DBhelper.connection.Close();
					result = list;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private static SQLiteConnection connection;
	}
}
